package com.tt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tt.entity.pojo.EchartsLineTrendBoxOfficePO;
import com.tt.entity.vo.EchartsLineTrendBoxOfficeVO;
import com.tt.mapper.EchartsLineTrendBoxOfficePOMapper;
import com.tt.service.EchartsLineTrendBoxOfficeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tt.util.HandleDecimal;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.ArrayList;


/**
 * <p>
 * Echarts票房趋势折线图表.需要定期更新.	此表记录只有一行,更新直接覆盖,不追加. 服务实现类
 * </p>
 *
 * @author TP
 * @since 2023-03-04 09:14:11
 */
@Service
public class EchartsLineTrendBoxOfficeServiceImpl extends ServiceImpl<EchartsLineTrendBoxOfficePOMapper, EchartsLineTrendBoxOfficePO> implements EchartsLineTrendBoxOfficeService {

    private final EchartsLineTrendBoxOfficePOMapper echartsLineTrendBoxOfficePOMapper;

    public EchartsLineTrendBoxOfficeServiceImpl(EchartsLineTrendBoxOfficePOMapper echartsLineTrendBoxOfficePOMapper) {
        this.echartsLineTrendBoxOfficePOMapper = echartsLineTrendBoxOfficePOMapper;
    }

    @Override
    public EchartsLineTrendBoxOfficeVO getEchartsLineTrend() {
        EchartsLineTrendBoxOfficeVO vo = new EchartsLineTrendBoxOfficeVO();

        // 票房折现趋势数据只有一份，数据库定死在第一行.
        EchartsLineTrendBoxOfficePO lineResult = echartsLineTrendBoxOfficePOMapper.selectOne(new QueryWrapper<>());

        ArrayList<Double> boxOffices = new ArrayList<>();
        ArrayList<LocalDate> dates = new ArrayList<>();

        boxOffices.add(HandleDecimal.handle(lineResult.getSixDaysAgo()));
        boxOffices.add(HandleDecimal.handle(lineResult.getFiveDaysAgo()));
        boxOffices.add(HandleDecimal.handle(lineResult.getFourDaysAgo()));
        boxOffices.add(HandleDecimal.handle(lineResult.getThreeDaysAgo()));
        boxOffices.add(HandleDecimal.handle(lineResult.getTwoDaysAgo()));
        boxOffices.add(HandleDecimal.handle(lineResult.getOneDaysAgo()));

        LocalDate date5 = lineResult.getLastDay();
        dates.add(date5.minusDays(5));
        dates.add(date5.minusDays(4));
        dates.add(date5.minusDays(3));
        dates.add(date5.minusDays(2));
        dates.add(date5.minusDays(1));
        dates.add(date5.minusDays(0));

        return vo.setBoxOffices(boxOffices).setDates(dates);
    }
}

